package com.ruoyi.carton.controller;


import com.ruoyi.carton.domain.bo.OrderOutboundBO;
import com.ruoyi.carton.domain.vo.OrderOutboundQuery;
import com.ruoyi.carton.domain.vo.OrderOutboundUpdateVO;
import com.ruoyi.carton.service.OrderOutboundService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/outbound")
public class OrderOutboundController extends BaseController {

    @Autowired
    private OrderOutboundService orderOutboundService;

    /**
     * 查询出库单详情
     *
     * @param id 出库单ID
     * @return 出库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:outbound:query')")
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Integer id) {
        OrderOutboundBO bo = orderOutboundService.getOutboundById(id);
        return AjaxResult.success(bo);
    }

    /**
     * 查询出库单列表（支持分页和查询条件）
     *
     * @param query 查询条件封装对象
     * @return 分页数据
     */
    @PreAuthorize("@ss.hasPermi('system:outbound:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderOutboundQuery query) {
        startPage();
        List<OrderOutboundBO> list = orderOutboundService.selectOutboundList(query);
        return getDataTable(list);
    }

    /**
     * 删除出库单，删除前判断状态是否允许，删除后回退订单状态
     *
     * @param id 出库单ID
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('system:outbound:remove')")
    @Log(title = "出库单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Integer id) {
        try {
            orderOutboundService.removeOutboundById(id);
            return AjaxResult.success("删除成功，订单状态已回退");
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改出库单信息
     *
     * @param orderOutboundUpdateVO 出库单更新视图对象
     * @return 操作结果
     */
    @PreAuthorize("@ss.hasPermi('system:outbound:edit')")
    @Log(title = "出库单管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult update(@RequestBody OrderOutboundUpdateVO orderOutboundUpdateVO) {
        try {
            orderOutboundService.updateOutboundById(orderOutboundUpdateVO);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            return AjaxResult.error("修改失败：" + e.getMessage());
        }
    }
}
